package xcmg.device.service.boxup;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.boxup.BoxupDetailDO;
import xcmg.device.dao.entity.boxup.BoxupMainDO;
import xcmg.device.dao.mapper.BoxupDetailMapper;
import xcmg.device.dao.mapper.BoxupMainMapper;
import xcmg.device.infra.DateUtil;
import xcmg.device.infra.EasyExcelUtil;
import xcmg.device.infra.ExportUtil;
import xcmg.device.service.vo.delivery.PrintDeliveryReceiptVO;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by wxf on 2018/6/12.
 */
@Service
public class BoxupDetailService {
    @Autowired
    private BoxupDetailMapper boxupDetailMapper;

    @Autowired
    private BoxupMainMapper boxupMainMapper;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private BoxupMainService boxupMainService;

    @Value( "${spring.file.download.path}" )
    private String filePath;

    public void export(String boxupId) throws Exception {
         BoxupMainDO boxupMainDO = boxupMainMapper.item(boxupId);
         List<BoxupDetailDO> boxupDetailDOList = boxupDetailMapper.detailExport(boxupId);
        executeExport(boxupDetailDOList,boxupMainDO);
    }

    public void executeExport(List<BoxupDetailDO> boxupDetailDOList,BoxupMainDO boxupMainDO) throws Exception {
        String title="装箱单";
        String[]subtitle={" 毛重/净重："+(boxupMainDO.getRoughWeight()==null?"":boxupMainDO.getRoughWeight())+"/"+(boxupMainDO.getWeight()==null?"":boxupMainDO.getWeight())+"kg"+"                       箱号:"+(boxupMainDO.getBoxupNo()==null?"":boxupMainDO.getBoxupNo()),
                " 装箱日期（DATE）:"+ DateUtil.formatDate(boxupMainDO.getCreateTime())};
        String[] rowsName = new String[]{"序号","零件号","零件名称","数量","单据号"};
        List<Object[]>  dataList = new ArrayList<Object[]>();
        for (int i = 0; i < boxupDetailDOList.size(); i++) {
            BoxupDetailDO bddo=boxupDetailDOList.get(i);
            Object[] objs = null;
            objs = new Object[rowsName.length];
            objs[0]= i+1;
            objs[1]=bddo.getMaterialsNo();
            objs[2]=bddo.getMaterialsDes();
            objs[3]=bddo.getQty();
            objs[4]=bddo.getDeliveryNoteNo();
            dataList.add(objs);
        }
        ExportUtil exportUtil=new ExportUtil(title,subtitle,rowsName,dataList);
        exportUtil.export(response);
    }


    public void export4Jck(String boxupId) throws Exception {
        easyExcelExport(boxupMainService.item4Export(boxupId));
    }

    public void easyExcelExport(PrintDeliveryReceiptVO receiptVO) throws Exception {
        //模板路径
        String templateFileName = filePath + "BoxupDetailTemplate.xlsx";

        String fileName = "装箱单";
        EasyExcelUtil.prepareResponse(response,fileName);
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        //如果有表尾，forceNewRow必须设置为true,但会所有数据都放入内存
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(receiptVO, fillConfig, writeSheet);
        excelWriter.fill(receiptVO.getMaterialsList(), fillConfig, writeSheet);
        excelWriter.fill(receiptVO, fillConfig, writeSheet);
        excelWriter.finish();
    }

}
